﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.model.Instance;
using l1cs.server.datatables;
using l1cs.server.templates;
using l1cs.server.utils;

namespace l1cs.server.model
{
    /// <summary>
    /// 血盟倉庫クラス
    /// </summary>
    public class L1DwarfForClanInventory : L1Inventory
    {
        private static Logger _log = Logger.getLogger( typeof( L1DwarfForClanInventory ).FullName );

        private L1Clan _clan;

        public L1DwarfForClanInventory( L1Clan clan )
        {
            _clan = clan;
        }

        // ＤＢのcharacter_itemsの読込
        /// <summary>
        /// DBから血盟倉庫のアイテムを読み込む
        /// </summary>
        public override void loadItems()
        {/*TTOK*/
            lock ( this )
            {
                String sqlstr = "SELECT * FROM clan_warehouse WHERE clan_name = ?1";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.String ).Value = _clan.getClanName();
                    foreach ( DataRow row in wrapper.GetDataTable( cmd ).Rows )
                    {
                        L1ItemInstance item = new L1ItemInstance();
                        int objectId = SqlUtils.toInt( row["id"] );
                        item.setId( objectId );
                        int itemId = SqlUtils.toInt( row["item_id"] );
                        L1Item itemTemplate = ItemTable.getInstance().getTemplate( itemId );
                        if ( itemTemplate == null )
                        {
                            throw new NullReferenceException( "item_id=" + itemId + " not found" );
                        }
                        item.setItem( itemTemplate );
                        item.setCount( SqlUtils.toInt( row["count"] ) );
                        item.setEquipped( false );
                        item.setEnchantLevel( SqlUtils.toInt( row["enchantlvl"] ) );
                        item.setIdentified( SqlUtils.toInt( row["is_id"] ) != 0 ? true : false );
                        item.set_durability( SqlUtils.toInt( row["durability"] ) );
                        item.setChargeCount( SqlUtils.toInt( row["charge_count"] ) );
                        item.setRemainingTime( SqlUtils.toInt( row["remaining_time"] ) );
                        item.setLastUsed( SqlUtils.toDateTime( row["last_used"] ) );
                        item.setBless( SqlUtils.toInt( row["bless"] ) );
                        item.setAttrEnchantKind( SqlUtils.toInt( row["attr_enchant_kind"] ) );
                        item.setAttrEnchantLevel( SqlUtils.toInt( row["attr_enchant_level"] ) );

                        _items.Add( item );

                        L1World.getInstance().storeObject( item );
                    }
                }
            }
        }

        /// <summary>
        /// 血盟倉庫にアイテムを追加する
        /// </summary>
        /// <param name="item">追加するアイテム</param>
        public override void insertItem( L1ItemInstance item )
        {/*TTOK*/
            lock ( this )
            {
                String sqlstr = @"INSERT INTO clan_warehouse SET 
                                    id                 = ?1 ,
                                    clan_name          = ?2 ,
                                    item_id            = ?3 ,
                                    item_name          = ?4 ,
                                    count              = ?5 ,
                                    is_equipped        = 0  ,
                                    enchantlvl         = ?6 ,
                                    is_id              = ?7 ,
                                    durability         = ?8 ,
                                    charge_count       = ?9 ,
                                    remaining_time     = ?10,
                                    last_used          = ?11,
                                    bless              = ?12,
                                    attr_enchant_kind  = ?13,
                                    attr_enchant_level = ?14";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = item.getId();
                    cmd.Parameters.Add( "?2", MySqlDbType.String ).Value = _clan.getClanName();
                    cmd.Parameters.Add( "?3", MySqlDbType.Int32 ).Value = item.getItemId();
                    cmd.Parameters.Add( "?4", MySqlDbType.String ).Value = item.getName();
                    cmd.Parameters.Add( "?5", MySqlDbType.Int32 ).Value = item.getCount();
                    cmd.Parameters.Add( "?6", MySqlDbType.Int32 ).Value = item.getEnchantLevel();
                    cmd.Parameters.Add( "?7", MySqlDbType.Int32 ).Value = item.isIdentified() ? 1 : 0;
                    cmd.Parameters.Add( "?8", MySqlDbType.Int32 ).Value = item.get_durability();
                    cmd.Parameters.Add( "?9", MySqlDbType.Int32 ).Value = item.getChargeCount();
                    cmd.Parameters.Add( "?10", MySqlDbType.Int32 ).Value = item.getRemainingTime();
                    cmd.Parameters.Add( "?11", MySqlDbType.DateTime ).Value = item.getLastUsed();
                    cmd.Parameters.Add( "?12", MySqlDbType.Int32 ).Value = item.getBless();
                    cmd.Parameters.Add( "?13", MySqlDbType.Int32 ).Value = item.getAttrEnchantKind();
                    cmd.Parameters.Add( "?14", MySqlDbType.Int32 ).Value = item.getAttrEnchantLevel();
                    wrapper.ExecNonQuery( cmd );
                }
            }
        }

        /// <summary>
        /// 血盟倉庫のアイテムを更新する
        /// </summary>
        /// <param name="item">更新するアイテム</param>
        public override void updateItem( L1ItemInstance item )
        {/*TTOK*/
            lock ( this )
            {
                String sqlstr = "UPDATE clan_warehouse SET count = ?1 WHERE id = ?2";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = item.getCount();
                    cmd.Parameters.Add( "?2", MySqlDbType.String ).Value = item.getId();
                    wrapper.ExecNonQuery( cmd );
                }
            }
        }

        /// <summary>
        /// 血盟倉庫からアイテムを削除する
        /// </summary>
        /// <param name="item">削除するアイテム</param>
        public override void deleteItem( L1ItemInstance item )
        {/*TTOK*/
            lock ( this )
            {
                String sqlstr = "DELETE FROM clan_warehouse WHERE id = ?1";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = item.getId();
                    wrapper.ExecNonQuery( cmd );
                }
                _items.Remove( item );
            }
        }

        /// <summary>
        /// DBのクラン倉庫のアイテムを全て削除(血盟解散時のみ使用)
        /// </summary>
        public void deleteAllItems()
        {/*TTOK*/
            lock ( this )
            {
                String sqlstr = "DELETE FROM clan_warehouse WHERE clan_name=?1";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.String ).Value = _clan.getClanName();
                    wrapper.ExecNonQuery( cmd );
                }
            }
        }
    }
}
